package com.smarthospital.web.configuration;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.auth.AutoUpdateCertificatesVerifier;
import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.CloseableHttpClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;

/**
 * create by sym  2021/5/4 23:48.
 */
@Configuration
public class SHConfiguration {
    @Value("${wx.miniApp.appid}")
    private String appid;
    @Value("${wx.miniApp.mchid}")
    private String mchid;
    @Value("${wx.miniApp.merchantSerialNumber}")
    private String merchantSerialNumber;
    @Value("${wx.miniApp.apiV3Key}")
    private String apiV3Key;

    @Value("${aliyun.sms.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.sms.accessKeySecret}")
    private String accessKeySecret;

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }


    @Bean
    public RestHighLevelClient elasticsearchClient() {

        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
//                .connectedTo("47.119.145.255:9200")
                .connectedTo("127.0.0.1:9200")
                .build();

        return RestClients.create(clientConfiguration).rest();
    }

    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }

    /**
     * 微信支付加签http客户端
     * @return
     * @throws FileNotFoundException
     * @throws UnsupportedEncodingException
     */
//    @Bean("wxPayHttpClient")
    public CloseableHttpClient  wxPayHttpClient() throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException {
        //加载本地密钥
//        PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(SHConfiguration.class.getResourceAsStream("wx/pem/apiclient_key.pem"));
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PrivateKey merchantPrivateKey = kf.generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode("RERBRkFGQUdBR0FHQUZBRkFEQVNEQURB")));
        //自动更新签名
        AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
                new WechatPay2Credentials(mchid, new PrivateKeySigner(merchantSerialNumber, merchantPrivateKey)),
                apiV3Key.getBytes("utf-8"));

        WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
                .withMerchant(mchid, merchantSerialNumber, merchantPrivateKey)
                .withValidator(new WechatPay2Validator(verifier));

        return builder.build();
    }


    /**
     * 阿里云 短信客户端
     * @return
     * @throws Exception
     */
    @Bean("aliSmsClient")
    public Client getClient() throws Exception {
            Config config = new Config()
                    // 您的AccessKey ID
                    .setAccessKeyId(accessKeyId)
                    // 您的AccessKey Secret
                    .setAccessKeySecret(accessKeySecret);
            // 访问的域名
            config.endpoint = "dysmsapi.aliyuncs.com";
            return new com.aliyun.dysmsapi20170525.Client(config);

    }


}
